package sn_gm_report

import org.DataUtils

import java.text.SimpleDateFormat

class EqpMonitorController {

    def exportFileService
    def dynamicQueryService

    def eqpMonitor() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }

        params.SYS_QUERY_NAME = 'Report_075eqpList'
        def eqpList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'eqpType'
        def eqptypeList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'moduleList'
        def ModuleList = dynamicQueryService.queryForList(params)


        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date().clearTime()))
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH");
        String strDate = sdf1.format(new Date());

        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        params.time = params.time ?: '1'
        def totalCount
        def tableHeadList = []
        def monitorList
        if (params.time == '2') {

            if (strDateH < 8) {
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.add(Calendar.HOUR_OF_DAY, -40);
                startTime = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, -16);
                endTime = ca1.getTime();

            } else {
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.add(Calendar.HOUR_OF_DAY, -16);
                startTime = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, 8);
                endTime = ca1.getTime();
            }

            params.startTime = sdf.format(startTime)
            params.endTime = sdf.format(endTime)
            params.SYS_QUERY_NAME = 'eqpMonitor'
            monitorList = dynamicQueryService.queryForList(params)

            if (monitorList.size() > 0) {
                tableHeadList = monitorList[0].keySet()
            }


            params.SYS_QUERY_NAME = 'eqpMonitorCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else {
            if (strDateH < 8) {
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.add(Calendar.HOUR_OF_DAY, -16);
                startTime = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, 8);
                endTime = ca1.getTime();

            } else {
                Calendar ca = Calendar.getInstance();
                ca.setTime(startTime);
                ca.add(Calendar.HOUR_OF_DAY, 8);
                startTime = ca.getTime();

                Calendar ca1 = Calendar.getInstance();
                ca1.setTime(endTime);
                ca1.add(Calendar.HOUR_OF_DAY, 32);
                endTime = ca1.getTime();

            }
            params.startTime = sdf.format(startTime)
            params.endTime = sdf.format(endTime)
            params.SYS_QUERY_NAME = 'lastEqpMonitor'
            monitorList = dynamicQueryService.queryForList(params)

            if (monitorList.size() > 0) {
                tableHeadList = monitorList[0].keySet()
            }

            params.SYS_QUERY_NAME = 'lastEqpMonitorCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }



        print(params)



        if (params.export == 'xls') {

            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [monitorList: monitorList, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)

        } else {
            [pageSizeList: DataUtils.getPageSizeList(), pageSize: pageSize, pageCurrent: pageCurrent, totalCount: totalCount,
             ModuleList  : ModuleList, eqptypeList: eqptypeList, eqpList: eqpList, monitorList: monitorList, tableHeadList: tableHeadList,
             startTime   : startTime, endTime: endTime, module: params.module ?: '', time: params.time ?: '', eqpType: params.eqpType ?: '',
             equipmentId : params.equipmentId ?: '']
        }
    }
    /**
     * D#
     * @return
     */
    def eqpWaitengTime() {

        params.SYS_QUERY_NAME = 'eqpWaitengTime'
        def eqpWaitengTime = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (eqpWaitengTime.size() > 0) {
            tableHeadList = eqpWaitengTime[0].keySet()
        }
        print(params)
        [eqpWaitengTime: eqpWaitengTime, start: params.startTime ?: '', tableHeadList: tableHeadList,
         end           : params.endTime ?: '', equipmentId: params.eqpid ?: '']
    }
    /**
     * eoh
     * @return
     */
    def eqpWipComponentID() {

        params.SYS_QUERY_NAME = 'eqpWipComponentID'
        def eqpWipComponentID = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (eqpWipComponentID.size() > 0) {
            tableHeadList = eqpWipComponentID[0].keySet()
        }
        print(params)
        [eqpWipComponentID: eqpWipComponentID, tableHeadList: tableHeadList,
         end              : params.endTime ?: '', eqpType: params.eqpType ?: '']
    }
    /**
     * RwipRlot
     * @return
     */
    def eqpRwipRlot() {

        params.SYS_QUERY_NAME = 'eqpRwipRlot'
        def eqpRwipRlot = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (eqpRwipRlot.size() > 0) {
            tableHeadList = eqpRwipRlot[0].keySet()
        }
        print(params)
        [eqpRwipRlot: eqpRwipRlot, start: params.startTime ?: '', tableHeadList: tableHeadList,
         end        : params.endTime ?: '', eqptype: params.eqptype ?: '']
    }
    /**
     * TTL
     * @return
     */
    def eqpTTL() {

        params.SYS_QUERY_NAME = 'eqpTTL'
        def eqpTTL = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (eqpTTL.size() > 0) {
            tableHeadList = eqpTTL[0].keySet()
        }

        print(params)
        [eqpTTL: eqpTTL, start: params.startTime ?: '', tableHeadList: tableHeadList,
         end   : params.endTime ?: '', eqpid: params.eqpid ?: '']
    }
}